import React, { Component } from "react";
import PubSub from "pubsub-js";
import axios from "axios";
export default class Search extends Component {
  //搜索的回调
  Search = () => {
    //获取用户的输入
    const {
      keyWordElement: { value: keyWord },
    } = this;
    PubSub.publish("updateSate", { isFirst: false, isLoading: true });
    //发送网络请求
    axios.get(`https://api.github.com/search/users?q=${keyWord}`).then(
      (response) => {
        console.log("成功了", response.data);
        PubSub.publish("updateSate", {
          isLoading: false,
          users: response.data.items,
        });
      },
      (error) => {
        PubSub.publish("updateSate", {
          isLoading: false,
          err: "请求出错",
        });
      }
    );
  };
  render() {
    return (
      <div>
        <section class="jumbotron">
          <h3 class="jumbotron-heading">搜索github用户</h3>
          <div>
            <input
              ref={(c) => (this.keyWordElement = c)}
              type="text"
              placeholder="输入关键词进行搜索"
            />
            &nbsp;<button onClick={this.Search}>搜索</button>
          </div>
        </section>
      </div>
    );
  }
}
